import ClientCookie
import urllib, urllib2
from urllib2 import URLError, HTTPError
import time
import re, csv, cPickle, datetime, pdb
import os

history_file = "stock_history.ksm"

def GetPage(address, form = {}):
    if type(form) != type({}):
        request = urllib2.Request(address)
    elif str(form) == str({}):
        request = urllib2.Request(address)
    else:
        qstring = urllib.urlencode(form) 
        request = urllib2.Request('%s%s' % (address,qstring)) 

    request.add_header('User-Agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8) Gecko/20050524 Fedora/1.5 Firefox/1.5')
    count = 0
    while(1):
        try:
            #print "GetPage - %s" % address
            response = ClientCookie.urlopen(request)
            page = response.read().decode("euc-kr")
            return page
        
        except URLError, e:
            print e
            if count == 10:
                break
            count += 1
            time.sleep(10)

    print "GetPage failed"
    return ""

def GetCodeFromName( name ):
    dic = {}
    dic["keyword"] = name
    addr = "http://stock.naver.com/sise/ksc_find_code.nhn?openfrom=&sosok=&%s"%urllib.urlencode(dic)
    page = GetPage(addr)
    print page
    rawstr = r"""<option value=([0-9]*)>[0-9]* ([^<]*)</option>"""
    match_obj = re.search(rawstr, page)
    if match_obj:
        all_groups = match_obj.groups()
    else:
        return ""
    return all_groups[0]

def GetStockValue(codeOrName):
    rec = re.compile(r"A[0-9]{6}")
    match_obj = rec.search(codeOrName)
    
    if not match_obj:
        code = GetCodeFromName( codeOrName )
    else:
        code = codeOrName

    address = "http://stock.finance.daum.net/search/quote/s2010000.html?code=%s" % code[1:]
    page = GetPage(address)

    rawstr = r"""<dl class="now">\s*
\s*<dt>[^<]*</dt>\s*
\s*<dd class="data1 [a-z]*">([0-9,]*)</dd>"""
    compile_obj = re.compile(rawstr, re.MULTILINE| re.UNICODE)
    match_obj = compile_obj.search(page)
    if match_obj:
        num = match_obj.group(1)
        num = int( "".join( num.split(",") ) )
        
        return num
    else:
        return -1

past_record = ""

def GetMyStock():
    global past_record

    print "GetMyStockaaaaaaaaaa!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    now = datetime.datetime.today()
    if now.weekday() >= 5 or now.time() > datetime.time(16, 0, 0, 0):
        if past_record != "":
            return past_record

    history = LoadData()
    if history:
        last_updated = history["dates"][-1]
        if now.date() != last_updated.date():
            GetAllValue()
        
    a = GetStockValue("A005490")
    b = GetStockValue("A003600")
    c = GetStockValue("A000520")
    duzon = GetStockValue("A045380")
    hanshin = GetStockValue("A004960")
    if a == -1 or b == -1 or c == -1 or duzon == -1 or hanshin == -1:
        return past_record
    else:
        past_record = ("Network").decode("utf-8").encode("euc-kr")
        return past_record




def SaveHistory (data):
    if not data["dates"]:
        return
    
    fo = open( history_file, "wb")
    version = 1.0
    cPickle.dump( version, fo)
    cPickle.dump( data, fo )
    fo.close()
    
def LoadData():
    try:
        fo = open( history_file, "rb")
    except IOError:
        fo.close()
        return None

    try:
        version = cPickle.load(fo)
        if version == 1.0:
            data = cPickle.load(fo)
            fo.close()
            return data
        else:
            print "Version error..."
    except:
        fo.close()

    return None

def GetNewReports(addr, period = 3):
    today = datetime.datetime.today()
    yester = today - datetime.timedelta(days=period) #days = 1
    str_today = "%4d%02d%02d"%(today.year,today.month,today.day)
    str_yester = "%4d%02d%02d"%(yester.year,yester.month,yester.day)
    
    addr += "&textBgnDt=%s&textEndDt=%s"%(str_yester,str_today)
    page = GetPage(addr)
    page = page.replace("\\\"", "\"")
    rawstr = r"""\t\t *vHtml \+= "(.*)";"""
    p = re.compile(rawstr)
    page = p.sub( r"\1", page )
    page = page.encode("euc-kr")
    
    rawstr = r"""javascript:fClickList\('([0-9]*)\',\'\'\);"""
    match_obj2 = re.findall(rawstr, page)
    for a in match_obj2:
        print a

    result = ""
    for a in match_obj2 :
        addr = "http://dart.fss.or.kr/frameForm.do?rcpNo=%s&dcmNo="%a
        os.system("rundll32 url.dll,FileProtocolHandler " + addr)

def GetCompanyReport( code ):
    addr = "http://dart.fss.or.kr/jsp/search/SearchCompany_M.jsp?textCrpNM=%s&selectSetTime=0&DspTp=&BsnTp=&hiddenWorkGuBun=select&hiddenFetchSize=15&hiddenCursorPosition=0&hiddenDataSize=&hiddenCurrentPage=1&hiddenCrpCode=&hiddenCrpCnt=0&hiddenSort=date&hiddenS" % code
    GetNewReports( addr )

def GetCompanyReports():
    GetCompanyReport("004960")
    GetCompanyReport("045380")
    GetCompanyReport("008490")

def GetBauPost():
    addr = "http://dart.fss.or.kr/jsp/search/Search_M.jsp?textCrpNM=&textFlrNM=TheBaupostGroup%%2CL.L.C.&selectSetTime=0&selectIndTp=all&selectCrpTp=all&selectAccMnt=all&textDcmNM=&DspTp=&BsnTp=&hiddenWorkGuBun=select&hiddenFetchSize=15&hiddenCursorPosition=0&"
    GetNewReports( addr )

# update every day...
def UpdateHistory(data_dic):
    now = datetime.datetime.today()
    if now.weekday() >= 5:
        return
    
    if now.time() < datetime.time(16, 0, 0, 0):
        return 
    #now = now + datetime.timedelta(days = -1)
    #pdb.set_trace()

    history = LoadData()
    if not history:
        for item in data_dic.iteritems():
            data_dic[item[0]] = [item[1]]
        data_dic["dates"] = [now]
        SaveHistory(data_dic)
        return

    last_updated = history["dates"][-1]
    if last_updated.date() == now.date():
        return

    for item in history.iteritems():
        if item[0] == "dates":
            history[item[0]].append(now)
            continue

        if data_dic.has_key(item[0]):
            history[item[0]].append(data_dic[item[0]])
        else:
            history[item[0]].append((0,0))

    for item in data_dic.iteritems():
        if not history.has_key(item[0]):
            history[item[0]] = [item[1]];
            
    print "history=" 
    print history
    #pdb.set_trace()
    SaveHistory(history)
    GetBauPost()
    GetCompanyReports()

last_result = ""
def GetAllValue():
    global last_result
    f = open("portfolio.csv")
    reader = csv.reader(f)
    
    result = ""
    result_sum = 0
    new_record = {}
    history = LoadData()

    now = datetime.datetime.today()
    if history and history.has_key("dates"):
        date_updated = history["dates"][-1]
        if now.date() == date_updated.date() and ( now.weekday() >= 5 or date_updated.time() > datetime.time(15, 30, 0, 0)):
            if last_result != "":
                return last_result

    for name, code, num in reader:
        num = int(num)
        if code.upper() == "CASH":
            val = 1;
        else:
            val = GetStockValue(code)

        if val == -1:
            return last_result
        
        if (not history) or (not history.has_key(code)):
            result += "%s\t%8d\n"%(name,val)
        else:
            result += "%s\t%8d%8d\n"%(name,val,val-history[code][-1][0])
        result_sum += val * num
        print name
        new_record[code] = (val, num)

    new_record["sum"] = result_sum
    UpdateHistory(new_record)

    if history:
        result += "\nTotal: %d (%d)"%(result_sum/10000, result_sum - history["sum"][-1])
    else:
        result += "\nTotal: %d"%(result_sum/10000)
    last_result = result
    return result#.decode("utf-8").encode("euc-kr")

#print GetAllValue()
